package controller

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/trainingManagementSystem/model"
	"strconv"
)

type RespondentController struct {
	Ctx  iris.Context
	Model *model.Questionnaire
}

func (r *RespondentController)PostInsertquestionnaire() mvc.Result {

	r.Model = new(model.Questionnaire)

	var q model.Questionnaire
	r.Ctx.ReadJSON(&q)

	res, err := r.Model.InsertQuestionnaire(&q)
	if err != nil{
		return mvc.Response{
		Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
	Object : map[string]interface{}{"message": "成功", "data": res, "code":200,},
	}
}

func (r *RespondentController)PostInsertquestionnaireasync() mvc.Result {

	r.Model = new(model.Questionnaire)

	var q model.Questionnaire
	r.Ctx.ReadJSON(&q)

	res, err := r.Model.InsertQuestionnaireAsync(&q)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":200,},
	}
}

func (r *RespondentController)PostQueryquestionnaire() mvc.Result {

	r.Model = new(model.Questionnaire)

	questionnaireIdStr := r.Ctx.FormValue("questionnaireId")
	questionnaireId, err := strconv.ParseInt(questionnaireIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := r.Model.QueryQuestionnaire(questionnaireId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":200,},
	}
}

func (r *RespondentController)PostSubmittheanswersheet() mvc.Result {

	r.Model = new(model.Questionnaire)

	var q model.Questionnaire
	r.Ctx.ReadJSON(&q)

	res, err := r.Model.SubmitTheAnswerSheet(&q)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":200,},
	}
}

func (r *RespondentController)PostSubmittheanswersheetasync() mvc.Result {

	r.Model = new(model.Questionnaire)

	var q model.Questionnaire
	r.Ctx.ReadJSON(&q)

	res, err := r.Model.SubmitTheAnswerSheetAsync(&q)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":200,},
	}
}

func (r *RespondentController)PostMarking() mvc.Result {

	r.Model = new(model.Questionnaire)

	questionnaireIdStr := r.Ctx.FormValue("questionnaireId")
	userIdStr := r.Ctx.FormValue("userId")

	questionnaireId, err := strconv.ParseInt(questionnaireIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := r.Model.Marking(questionnaireId, userId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":200,},
	}
}

func (r *RespondentController)PostMarkingasync() mvc.Result {

	r.Model = new(model.Questionnaire)

	questionnaireIdStr := r.Ctx.FormValue("questionnaireId")
	userIdStr := r.Ctx.FormValue("userId")

	questionnaireId, err := strconv.ParseInt(questionnaireIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := r.Model.MarkingAsync(questionnaireId, userId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":200,},
	}
}

















